using System;
using System.Data;
using System.Collections;
using System.Resources;
using System.Windows.Forms;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.Framework.ReportFrame.BO;
using PCSComMaterials.Inventory.BO;
using PCSComMaterials.Inventory.DS;
using PCSComUtils.PCSExc;
using PCSUtils.Utils;
using PCSUtils.Log;
using PCSUtils.Framework.ReportFrame;

namespace PCSMaterials.Inventory
{
    /// <summary>
    /// Summary description for IVInventoryAdjustment.
    /// </summary>
    public partial class IVInventoryAdjustment : Form
    {
        const string THIS = "PCSMaterials.Inventory.IVInventoryAdjustment";
        EnumAction formMode;
        UtilsBO boUtil = new UtilsBO();
        private const string V_PRODUCTINVENTORYADJUSTMENT = "V_ProductInventoryAdjustment";
        private const string V_LOTBYBIN = "V_LOTBYBIN";
        private const string V_LOTBYLOC = "V_LOTBYLOC";
        private const string V_IVADJUSTMENTANDPRODUCT = "V_IVAdjustmentAndProduct";
        private const string PARTNUMBER = "PartNumber";
        private const string PARTNAME = "PartName";
        private const string MODEL = "Model";
        private const string MASLOCNAME = "MasLocName";
        private const string LOCNAME = "LocName";
        private const string BINNAME = "BinName";
        private const string UM = "UM";
        private const string AVAILABLE_QUANTITY = "AvailQuantity";
        private const string ISSUE_QUANTITY = "IssueQuantity";
        private bool blnLotControl;
        private bool blnBinControl;
        private bool blnHasError;
        DateTime dtmCurrentDate;
        private IV_AdjustmentVO voIV_Adjustment = new IV_AdjustmentVO();
        
        public IVInventoryAdjustment()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
        }

        /// <summary>
        /// IVInventoryAddjustment_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void IVInventoryAddjustment_Load(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".IVInventoryAddjustment_Load()";
            try
            {
                //Set authorization for user
                Security objSecurity = new Security();
                this.Name = THIS;
                if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    this.Close();
                    // You don't have the right to view this item
                    PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
                    return;
                }
                // Load combo box

                DataSet dstCCN = boUtil.ListCCN();
                cboCCN.DataSource = dstCCN.Tables[MST_CCNTable.TABLE_NAME];
                cboCCN.DisplayMember = MST_CCNTable.CODE_FLD;
                cboCCN.ValueMember = MST_CCNTable.CCNID_FLD;
                FormControlComponents.PutDataIntoC1ComboBox(cboCCN, dstCCN.Tables[MST_CCNTable.TABLE_NAME], MST_CCNTable.CODE_FLD, MST_CCNTable.CCNID_FLD, MST_CCNTable.TABLE_NAME);
                if (SystemProperty.CCNID != 0)
                {
                    cboCCN.SelectedValue = SystemProperty.CCNID;
                }
                dtmPostDate.CustomFormat = Constants.DATETIME_FORMAT_HOUR;
                dtmCurrentDate = (new UtilsBO()).GetDBDate().AddDays(1);
                //switch form mode
                formMode = EnumAction.Default;
                SwitchFormMode();
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// ClearForm
        /// </summary>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void ClearForm()
        {
            const string METHOD_NAME = THIS + ".ClearForm()";
            try
            {
                txtAdjustQuantity.Value = null;
                txtAvailQuantity.Value = null;
                txtComment.Text = string.Empty;
                txtLot.Text = string.Empty;
                txtModel.Text = string.Empty;
                txtPartName.Text = string.Empty;
                txtPartNumber.Text = string.Empty;
                txtQAStatus.Text = string.Empty;
                txtSerial.Text = string.Empty;
                txtTransNo.Text = string.Empty;
                txtMasLoc.Text = string.Empty;
                txtLocation.Text = string.Empty;
                txtBin.Text = string.Empty;
                txtUM.Text = string.Empty;
                chkUsedByCosting.Checked = false;
            }
            catch (PCSException ex)
            {
                throw new PCSException(ex.mCode, METHOD_NAME, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        /// <summary>
        /// SwitchFormMode
        /// </summary>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void SwitchFormMode()
        {
            switch (formMode)
            {
                case EnumAction.Default:
                    dtmPostDate.Enabled = false;
                    txtComment.Enabled = false;
                    txtAdjustQuantity.Enabled = false;
                    txtAvailQuantity.Enabled = false;
                    txtLot.Enabled = false;
                    txtModel.Enabled = false;
                    txtPartName.Enabled = false;
                    txtPartNumber.Enabled = false;
                    txtQAStatus.Enabled = false;
                    txtSerial.Enabled = false;
                    txtLocation.Enabled = false;
                    txtMasLoc.Enabled = false;
                    txtBin.Enabled = false;
                    btnBin.Enabled = false;
                    btnLocation.Enabled = false;
                    btnLot.Enabled = false;
                    btnMasLoc.Enabled = false;
                    btnPartName.Enabled = false;
                    btnPartNumber.Enabled = false;
                    btnSave.Enabled = false;
                    btnAdd.Enabled = true;
                    UpdateInventoryData.Enabled = true;
                    btnTransNo.Enabled = true;
                    chkUsedByCosting.Enabled = false;
                    if (txtTransNo.Text.Trim() != string.Empty)
                    {
                        btnDelete.Enabled = true;
                        btnDelTran.Enabled = true;
                    }
                    else
                    {
                        btnDelete.Enabled = false;
                        btnDelTran.Enabled = false;
                    }
                    break;
                case EnumAction.Add:
                    dtmPostDate.Enabled = true;
                    txtComment.Enabled = true;
                    txtAdjustQuantity.Enabled = true;
                    txtLot.Enabled = true;
                    txtModel.Enabled = false;
                    txtPartName.Enabled = true;
                    txtPartNumber.Enabled = true;
                    txtQAStatus.Enabled = false;
                    txtSerial.Enabled = true;
                    txtLocation.Enabled = true;
                    txtMasLoc.Enabled = true;
                    txtBin.Enabled = true;
                    btnBin.Enabled = true;
                    btnLocation.Enabled = true;
                    btnLot.Enabled = true;
                    btnMasLoc.Enabled = true;
                    btnPartName.Enabled = true;
                    btnPartNumber.Enabled = true;
                    btnSave.Enabled = true;
                    cboCCN.Enabled = true;
                    btnAdd.Enabled = false;
                    btnTransNo.Enabled = false;
                    chkUsedByCosting.Enabled = true;
                    chkUsedByCosting.Checked = false;
                    btnDelete.Enabled = false;
                    btnDelTran.Enabled = false;
                    UpdateInventoryData.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// ClearSomeControls
        /// </summary>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void ClearSomeControls()
        {
            const string METHOD_NAME = THIS + ".ClearSomeControls()";
            try
            {
                //txtMasLoc.Text = string.Empty;
                txtLocation.Text = string.Empty;
                txtBin.Text = string.Empty;
                txtAvailQuantity.Value = null;
                txtQAStatus.Text = string.Empty;
                txtLot.Text = string.Empty;
                txtSerial.Text = string.Empty;
            }
            catch (PCSException ex)
            {
                throw new PCSException(ex.mCode, METHOD_NAME, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".IVInventoryAddjustment_Load()";
            try
            {
                //Switch form Mode
                formMode = EnumAction.Add;
                ClearForm();
                SwitchFormMode();
                // Load PostDate
                IV_AdjustmentVO voIV_Adjustment = new IV_AdjustmentVO();
                voIV_Adjustment.PostDate = boUtil.GetDBDate();
                if ((DateTime.MinValue < voIV_Adjustment.PostDate) && (voIV_Adjustment.PostDate < DateTime.MaxValue))
                    dtmPostDate.Value = voIV_Adjustment.PostDate;
                else
                    dtmPostDate.Value = DBNull.Value;
                //Fill Transfer Number
                //txtTransNo.Text = boUtil.GetNoByMask(IV_AdjustmentTable.TABLE_NAME, IV_AdjustmentTable.TRANSNO_FLD, DateTime.Parse(dtmPostDate.Value.ToString()), string.Empty);
                txtTransNo.Text = FormControlComponents.GetNoByMask(this);
                //Fill Default Master Location 
                FormControlComponents.SetDefaultMasterLocation(txtMasLoc);
                voIV_Adjustment.MasterLocationID = SystemProperty.MasterLocationID;
                //Set focus to PostDate
                dtmPostDate.Focus();
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// IsValidateData
        /// </summary>
        /// <returns></returns>
        /// <author>Trada</author>
        /// <date>Tuesday, July 26 2005</date>
        private bool IsValidateData()
        {
            const string METHOD_NAME = THIS + ".IsValidateData()";
            try
            {
                //Check Mandatory
                if (FormControlComponents.CheckMandatory(dtmPostDate))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return false;
                }

                if (!FormControlComponents.CheckDateInCurrentPeriod(DateTime.Parse(dtmPostDate.Value.ToString())))
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGV_POSTDATE_PERIOD);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return false;
                }

                //check the PostDate smaller than the current date
                if ((DateTime)dtmPostDate.Value > new UtilsBO().GetDBDate())
                {
                    //MessageBox.Show("The Post Date you input is not in the current period");
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INV_TRANSACTION_CANNOT_IN_FUTURE, MessageBoxIcon.Warning);
                    dtmPostDate.Focus();
                    return false;
                }

                if (FormControlComponents.CheckMandatory(txtTransNo))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtTransNo.Focus();
                    txtTransNo.Select();
                    return false;
                }
                if (FormControlComponents.CheckMandatory(txtPartNumber))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtPartNumber.Focus();
                    txtPartNumber.Select();
                    return false;
                }
                if (FormControlComponents.CheckMandatory(txtPartName))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtPartName.Focus();
                    txtPartName.Select();
                    return false;
                }
                if (FormControlComponents.CheckMandatory(txtMasLoc))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtMasLoc.Focus();
                    txtMasLoc.Select();
                    return false;
                }
                if (FormControlComponents.CheckMandatory(txtLocation))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtLocation.Focus();
                    txtLocation.Select();
                    return false;
                }
                if (FormControlComponents.CheckMandatory(txtAdjustQuantity))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    txtAdjustQuantity.Focus();
                    txtAdjustQuantity.Select();
                    return false;
                }
                if ((decimal)txtAdjustQuantity.Value == 0)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_MATERIALRECEIPT_QUANTITY_HIGHER_0);
                    txtAdjustQuantity.Focus();
                    return false;
                }

                if (blnLotControl)
                {
                    if (txtLot.Text.Trim() == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_IV_ADJUSTMENT_PLS_INPUT_LOT);
                        txtLot.Focus();
                        txtLot.Select();
                        return false;
                    }
                }
                if (blnBinControl)
                {
                    if (txtBin.Text.Trim() == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_IV_ADJUSTMENT_PLS_INPUT_BIN);
                        txtBin.Focus();
                        txtBin.Select();
                        return false;
                    }
                }
                if ((txtAvailQuantity.Value != null) && (txtAvailQuantity.Value != DBNull.Value))
                {
                    if ((decimal.Parse(txtAdjustQuantity.Value.ToString()) + decimal.Parse(txtAvailQuantity.Value.ToString())) < 0)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_IV_ADJUSTMENT_ADJUSTQTY_MUST_BE_SMALLER_THAN_AVAILABLEQTY, MessageBoxIcon.Warning);
                        txtAdjustQuantity.Focus();
                        txtAdjustQuantity.Select();
                        return false;
                    }
                }
                //Check postdate in configuration
                if (!FormControlComponents.CheckPostDateInConfiguration((DateTime)dtmPostDate.Value))
                {
                    return false;
                }
                return true;
            }
            catch (PCSException ex)
            {
                throw new PCSException(ex.mCode, METHOD_NAME, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        /// <summary>
        /// btnSave_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnSave_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnSave_Click()";
            try
            {
                if (PCSMessageBox.Show(ErrorCode.MESSAGE_CONFIRM_BEFORE_SAVE_WOCOMPLETION, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new string[] { dtmPostDate.Text }) == DialogResult.Yes)
                {
                    blnHasError = true;
                    if (IsValidateData())
                    {
                        if (Security.IsDifferencePrefix(this, lblTransNo, txtTransNo))
                        {
                            return;
                        }

                        int pintIV_Adjustment;
                        voIV_Adjustment.AdjustQuantity = decimal.Parse(txtAdjustQuantity.Value.ToString());
                        if (txtAvailQuantity.Value.ToString() != string.Empty)
                            voIV_Adjustment.AvailableQuantity = Convert.ToDecimal(txtAvailQuantity.Value);
                        if (txtBin.Tag != null)
                        {
                            voIV_Adjustment.BinID = int.Parse(txtBin.Tag.ToString());
                        }
                        else
                            voIV_Adjustment.BinID = 0;
                        voIV_Adjustment.CCNID = int.Parse(cboCCN.SelectedValue.ToString());
                        voIV_Adjustment.Comment = txtComment.Text;
                        voIV_Adjustment.LocationID = int.Parse(txtLocation.Tag.ToString());
                        voIV_Adjustment.Lot = txtLot.Text;
                        //voIV_Adjustment.MasterLocationID = voMasLoc.MasterLocationID;
                        //Set Date only
                        DateTime dtmPostDateOnly = new DateTime(DateTime.Parse(dtmPostDate.Value.ToString()).Year,
                            DateTime.Parse(dtmPostDate.Value.ToString()).Month, DateTime.Parse(dtmPostDate.Value.ToString()).Day,
                            DateTime.Parse(dtmPostDate.Value.ToString()).Hour, DateTime.Parse(dtmPostDate.Value.ToString()).Minute, 0);
                        //voIV_Adjustment.PostDate = DateTime.Parse(dtmPostDate.Value.ToString());
                        voIV_Adjustment.PostDate = dtmPostDateOnly;
                        voIV_Adjustment.Serial = txtSerial.Text;
                        voIV_Adjustment.TransNo = txtTransNo.Text;
                        // 20-06-2006 dungla add one more field
                        voIV_Adjustment.UsedByCosting = chkUsedByCosting.Checked;
                        IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                        pintIV_Adjustment = boIVInventoryAdjustment.AddAndReturnID(voIV_Adjustment);
                        Security.UpdateUserNameModifyTransaction(this, IV_AdjustmentTable.ADJUSTMENTID_FLD, pintIV_Adjustment);
                        PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);
                        btnSave.Enabled = false;
                        btnAdd.Enabled = true;
                        //ClearForm();
                        formMode = EnumAction.Default;
                        blnHasError = false;
                        SwitchFormMode();
                        btnAdd.Focus();

                        //HACK: added by Tuan TQ- 12 July, 2006- Store TransID for printing
                        txtTransNo.Tag = pintIV_Adjustment;
                        //End hack
                    }
                }
            }
            catch (PCSException ex)
            {

                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                if (ex.mCode == ErrorCode.DUPLICATE_KEY)
                {
                    txtTransNo.Focus();
                    txtTransNo.Select();
                }
                try
                {

                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }

            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        #region Print Adjustment Slip - Tuan TQ
        /// <summary>
        /// Build and show Inventory Adjustment Slip Report
        /// </summary>
        /// <Author> Tuan TQ, 12 July, 2006</Author>
        private void ShowAdjustmentSlipReport(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".ShowAdjustmentSlipReport()";
            try
            {
                const string REPORT_NAME = "InventoryAdjustmentReport";
                const string REPORT_LAYOUT_FILE = "InventoryAdjustmentReport.xml";
                const string REPORTFLD_TITLE = "fldTitle";

                const string APPLICATION_PATH = @"PCSMain\bin\Debug";

                const string REPORTFLD_COMPANY = "fldCompany";

                //return if no record was selected
                if (txtTransNo.Tag == null)
                {
                    return;
                }

                int intMasterID;
                intMasterID = int.Parse(txtTransNo.Tag.ToString());
                if (intMasterID <= 0)
                {
                    return;
                }

                //Change cursor to wait status
                this.Cursor = Cursors.WaitCursor;

                C1PrintPreviewDialog printPreview = new C1PrintPreviewDialog();
                C1PrintPreviewDialogBO boDataReport = new C1PrintPreviewDialogBO();

                DataTable dtbResult;
                dtbResult = boDataReport.GetInventoryAdjustmentData(intMasterID);

                ReportBuilder reportBuilder = new ReportBuilder();

                //Get actual application path
                string strReportPath = Application.StartupPath;
                int intIndex = strReportPath.IndexOf(APPLICATION_PATH);
                if (intIndex > -1)
                {
                    strReportPath = strReportPath.Substring(0, intIndex);
                }

                if (strReportPath.Substring(strReportPath.Length - 1) == @"\")
                {
                    strReportPath += Constants.REPORT_DEFINITION_STORE_LOCATION;
                }
                else
                {
                    strReportPath += "\\" + Constants.REPORT_DEFINITION_STORE_LOCATION;
                }

                //Set datasource and lay-out path for reports
                reportBuilder.ReportName = REPORT_NAME;
                reportBuilder.SourceDataTable = dtbResult;
                reportBuilder.ReportDefinitionFolder = strReportPath;

                reportBuilder.ReportLayoutFile = REPORT_LAYOUT_FILE;

                //check if layout is valid
                if (reportBuilder.AnalyseLayoutFile())
                {
                    reportBuilder.UseLayoutFile = true;
                }
                else
                {
                    //Set cursor to default
                    this.Cursor = Cursors.Default;
                    PCSMessageBox.Show(ErrorCode.MESSAGE_REPORT_TEMPLATE_FILE_NOT_FOUND, MessageBoxIcon.Error);
                    return;
                }

                reportBuilder.MakeDataTableForRender();

                // and show it in preview dialog
                reportBuilder.ReportViewer = printPreview.ReportViewer;
                reportBuilder.RenderReport();

                //Header information get from system params				
                reportBuilder.DrawPredefinedField(REPORTFLD_COMPANY, SystemProperty.SytemParams.Get(SystemParam.COMPANY_FULL_NAME));

                reportBuilder.RefreshReport();

                //Print report
                try
                {
                    printPreview.FormTitle = reportBuilder.GetFieldByName(REPORTFLD_TITLE).Text;
                }
                catch
                { }

                printPreview.Show();
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            finally
            {
                //Set cursor to default status
                this.Cursor = Cursors.Default;
            }
        }


        /// <summary>
        /// btnPrint_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Tuan TQ</author>
        /// <date>Wednesday, July 12 2006</date>
        private void btnPrint_Click(object sender, System.EventArgs e)
        {
            //ImportBeginQuantity frmImport = new ImportBeginQuantity();
            //frmImport.Show();
            //ShowAdjustmentSlipReport(sender, e);
        }
        #endregion

        /// <summary>
        /// btnClose_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnClose_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".IVInventoryAddjustment_Load()";
            try
            {
                this.Close();
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// btnTransNo_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnTransNo_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnTransNo_Click()";
            try
            {
                Hashtable htbCriteria = new Hashtable();
                DataRowView drwResult = null;
                if (cboCCN.SelectedValue != null)
                {
                    htbCriteria.Add(IV_AdjustmentTable.CCNID_FLD, cboCCN.SelectedValue);
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                }
                drwResult = FormControlComponents.OpenSearchForm(V_IVADJUSTMENTANDPRODUCT, IV_AdjustmentTable.TRANSNO_FLD, txtTransNo.Text.Trim(), htbCriteria, true);
                if (drwResult != null)
                {
                    //HACK: added by Tuan TQ- 12 July, 2006- Store TransID for printing
                    txtTransNo.Tag = drwResult[IV_AdjustmentTable.ADJUSTMENTID_FLD];
                    //End hack

                    txtTransNo.Text = drwResult[IV_AdjustmentTable.TRANSNO_FLD].ToString();
                    txtComment.Text = drwResult[IV_AdjustmentTable.COMMENT_FLD].ToString();
                    dtmPostDate.Value = drwResult[IV_AdjustmentTable.POSTDATE_FLD];
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                    txtLocation.Text = drwResult[LOCNAME].ToString();
                    txtUM.Text = drwResult[UM].ToString();
                    if (drwResult[BINNAME] != DBNull.Value)
                    {
                        txtBin.Text = drwResult[BINNAME].ToString();
                    }
                    if (drwResult[IV_AdjustmentTable.LOT_FLD] != DBNull.Value)
                    {
                        txtLot.Text = drwResult[IV_AdjustmentTable.LOT_FLD].ToString();
                    }
                    if (drwResult[IV_AdjustmentTable.SERIAL_FLD] != DBNull.Value)
                    {
                        txtSerial.Text = drwResult[IV_AdjustmentTable.SERIAL_FLD].ToString();
                    }
                    txtAdjustQuantity.Value = drwResult[IV_AdjustmentTable.ADJUSTQUANTITY_FLD];
                    txtAvailQuantity.Value = drwResult[IV_AdjustmentTable.AVAILABLEQTY_FLD];
                    try
                    {
                        chkUsedByCosting.Checked = Convert.ToBoolean(drwResult["UsedByCosting"]);
                    }
                    catch
                    {
                        chkUsedByCosting.Checked = false;
                    }
                    SwitchFormMode();
                }

            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// Fill available Quantity if we have Bin and Location
        /// </summary>
        /// <param name="pintProductID"></param>
        /// <author>Trada</author>
        /// <date>Friday, September 8 2006</date>
        private void FillAvailableQuantity(int pintProductID)
        {
            if (txtBin.Text != string.Empty && txtLocation.Text != string.Empty)
            {
                decimal decAvailableQuantity = new InventoryUtilsBO().GetAvailableQtyByPostDate(dtmCurrentDate, int.Parse(cboCCN.SelectedValue.ToString()),
                    voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), (int)txtBin.Tag, pintProductID);
                txtAvailQuantity.Value = decAvailableQuantity;
            }
        }
        /// <summary>
        /// btnPartNumber_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnPartNumber_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnPartNumber_Click()";
            try
            {
                if (FormControlComponents.CheckMandatory(dtmPostDate))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return;
                }
                //Search Part Number
                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(V_PRODUCTINVENTORYADJUSTMENT, PARTNUMBER, txtPartNumber.Text.Trim(), null, true);
                if (drwResult != null)
                {
                    voIV_Adjustment.ProductID = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    voIV_Adjustment.StockUMID = int.Parse(drwResult[ITM_ProductTable.STOCKUMID_FLD].ToString());
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    if (drwResult[MASLOCNAME] != DBNull.Value)
                    {
                        txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                        voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());
                    }
                    else
                        voIV_Adjustment.MasterLocationID = 0;
                    if (drwResult[UM] != DBNull.Value)
                    {
                        txtUM.Text = drwResult[UM].ToString();
                        voIV_Adjustment.StockUMID = int.Parse(drwResult[ITM_ProductTable.STOCKUMID_FLD].ToString());
                    }
                    //Check Available if have Bin and Location
                    FillAvailableQuantity(voIV_Adjustment.ProductID);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// btnPartName_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnPartName_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnPartName_Click()";
            try
            {
                if (FormControlComponents.CheckMandatory(dtmPostDate))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return;
                }
                //Search Part Name
                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(V_PRODUCTINVENTORYADJUSTMENT, PARTNAME, txtPartName.Text.Trim(), null, true);
                if (drwResult != null)
                {
                    voIV_Adjustment.ProductID = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    if (drwResult[MASLOCNAME] != DBNull.Value)
                    {
                        txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                        voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());
                    }
                    else
                        voIV_Adjustment.MasterLocationID = 0;
                    if (drwResult[UM] != DBNull.Value)
                    {
                        txtUM.Text = drwResult[UM].ToString();
                        voIV_Adjustment.StockUMID = int.Parse(drwResult[ITM_ProductTable.STOCKUMID_FLD].ToString());
                    }
                    //Check Available if have Bin and Location
                    FillAvailableQuantity(voIV_Adjustment.ProductID);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// btnBin_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnBin_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnBin_Click()";
            try
            {
                Hashtable htbCriteria = new Hashtable();
                if (txtLocation.Text.Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_SELECTLOC, MessageBoxIcon.Error);
                    txtLocation.Focus();
                    txtLocation.Select();
                    return;
                }
                else
                    htbCriteria.Add(MST_BINTable.LOCATIONID_FLD, int.Parse(txtLocation.Tag.ToString()));

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(MST_BINTable.TABLE_NAME, MST_BINTable.CODE_FLD, txtBin.Text.Trim(), htbCriteria, true);
                if (drwResult != null)
                {
                    txtBin.Text = drwResult[MST_BINTable.CODE_FLD].ToString();
                    txtBin.Tag = drwResult[MST_BINTable.BINID_FLD];

                    //Get available quantity where product
                    //					IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    //					dstAvailableQtyAndInsStatus = boIVInventoryAdjustment.GetAvalableQuantity(int.Parse(cboCCN.SelectedValue.ToString()),
                    //						voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), int.Parse(txtBin.Tag.ToString()), voIV_Adjustment.ProductID);
                    if (txtPartNumber.Text != string.Empty)
                    {
                        decimal decAvailableQuantity = new InventoryUtilsBO().GetAvailableQtyByPostDate(dtmCurrentDate, int.Parse(cboCCN.SelectedValue.ToString()),
                            voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), (int)txtBin.Tag, voIV_Adjustment.ProductID);
                        txtAvailQuantity.Value = decAvailableQuantity;
                    }
                    //					if (dstAvailableQtyAndInsStatus.Tables[0].Rows.Count > 0)
                    //					{
                    //						txtAvailQuantity.Value = decAvailableQuantity;
                    //						txtQAStatus.Text = dstAvailableQtyAndInsStatus.Tables[0].Rows[0][IV_LocationCacheTable.INSPSTATUS_FLD].ToString();
                    //					}
                    //					else
                    //					{
                    //						txtAvailQuantity.Value = 0;
                    //						txtQAStatus.Text = string.Empty;
                    //					}

                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// btnLocation_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnLocation_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnLocation_Click()";
            try
            {
                Hashtable htbCriteria = new Hashtable();
                if (txtMasLoc.Text.Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MSG_LABORTIME_SELECTMASLOC, MessageBoxIcon.Error);
                    txtMasLoc.Focus();
                    txtMasLoc.Select();
                    return;
                }
                else
                    htbCriteria.Add(MST_LocationTable.MASTERLOCATIONID_FLD, voIV_Adjustment.MasterLocationID);

                DataRowView drwResult = FormControlComponents.OpenSearchForm(MST_LocationTable.TABLE_NAME, MST_LocationTable.CODE_FLD, txtLocation.Text.Trim(), htbCriteria, true);
                if (drwResult != null)
                {
                    if (txtLocation.Tag != null)
                    {
                        if (txtLocation.Tag.ToString() != drwResult[MST_LocationTable.LOCATIONID_FLD].ToString())
                        {
                            //ClearForm();
                            ClearSomeControls();
                        }
                    }

                    txtLocation.Tag = drwResult[MST_LocationTable.LOCATIONID_FLD];
                    txtLocation.Text = drwResult[MST_LocationTable.CODE_FLD].ToString();

                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// btnMasLoc_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void btnMasLoc_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnMasLoc_Click()";
            try
            {
                DataRowView drwResult = null;
                Hashtable htbCriteria = new Hashtable();
                #region // HACK: DEL Trada 10-14-2005

                //				if (txtPartNumber.Text.Trim() == string.Empty)
                //				{
                //					PCSMessageBox.Show(ErrorCode.MESSAGE_IV_ADJUSTMENT_PLS_INPUT_PATHNUMBER, MessageBoxIcon.Warning);
                //					txtPartNumber.Focus();
                //					return;
                //				}
                //				else 
                //					htbCriteria.Add(ITM_ProductTable.PRODUCTID_FLD, voIV_Adjustment.ProductID);

                #endregion // END: DEL Trada 10-14-2005

                drwResult = FormControlComponents.OpenSearchForm(MST_MasterLocationTable.TABLE_NAME, MST_MasterLocationTable.CODE_FLD, txtMasLoc.Text.Trim(), htbCriteria, true);
                if (drwResult != null)
                {
                    if (voIV_Adjustment.MasterLocationID != 0)
                    {
                        if (voIV_Adjustment.MasterLocationID != int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString()))
                        {
                            //lblMasLoc.Tag = drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD];
                            //ClearForm();
                            ClearSomeControls();
                        }

                    }
                    #region HACK: DEL Trada 10-17-2005

                    //					if (txtMasLoc.Tag != null)
                    //					{
                    //						if (txtMasLoc.Tag.ToString() != drwResult[PRODUCT_CODE].ToString())
                    //						{
                    //
                    //							txtMasLoc.Tag = drwResult[PRODUCT_CODE];
                    //							//ClearForm();
                    //							ClearSomeControls();
                    //						}
                    //					}

                    #endregion END: DEL Trada 10-17-2005
                    //lblMasLoc.Tag = drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD];
                    voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());
                    #region HACK: DEL Trada 10-17-2005

                    //					txtMasLoc.Tag = drwResult[PRODUCT_CODE];

                    #endregion END: DEL Trada 10-17-2005
                    txtMasLoc.Text = drwResult[MST_MasterLocationTable.CODE_FLD].ToString();
                    #region HACK: DEL Trada 10-17-2005

                    //					txtAvailQuantity.Value = drwResult[AVAILABLE_QUANTITY];

                    #endregion END: DEL Trada 10-17-2005
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtPartName_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void txtPartName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtPartName_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnPartName_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtPartNumber_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void txtPartNumber_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtPartNumber_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnPartNumber_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }

        }

        /// <summary>
        /// txtMasLoc_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 22 2005</date>
        private void txtMasLoc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtMasLoc_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnMasLoc_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// btnLot_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Wednesday, July 27 2005</date>
        private void btnLot_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnLot_Click()";
            try
            {
                Hashtable htbCriteria = new Hashtable();
                DataRowView drwResult = null;
                if (blnBinControl)
                {
                    if (txtBin.Text == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_MUST_SELECT_BIN_FOR_LOCATION, MessageBoxIcon.Warning);
                        txtBin.Focus();
                        return;
                    }
                    else
                        htbCriteria.Add(MST_BINTable.BINID_FLD, int.Parse(txtBin.Tag.ToString()));
                    htbCriteria.Add(ITM_ProductTable.PRODUCTID_FLD, voIV_Adjustment.ProductID);
                    drwResult = FormControlComponents.OpenSearchForm(V_LOTBYBIN, IV_BinCacheTable.LOT_FLD, txtLot.Text.Trim(), htbCriteria, true);
                    if (drwResult != null)
                    {
                        txtLot.Text = drwResult[IV_BinCacheTable.LOT_FLD].ToString();
                        txtSerial.Text = drwResult[IV_ItemSerialTable.SERIAL_FLD].ToString();
                        txtAvailQuantity.Value = drwResult[ISSUE_QUANTITY];
                    }
                }
                else
                {
                    if (txtLocation.Text == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_MATERIALRECEIPT_SELECT_LOCATION, MessageBoxIcon.Warning);
                        txtLocation.Focus();
                        return;
                    }
                    else
                        htbCriteria.Add(MST_LocationTable.LOCATIONID_FLD, int.Parse(txtLocation.Tag.ToString()));
                    htbCriteria.Add(ITM_ProductTable.PRODUCTID_FLD, voIV_Adjustment.ProductID);
                    drwResult = FormControlComponents.OpenSearchForm(V_LOTBYLOC, IV_LocationCacheTable.LOT_FLD, txtBin.Text.Trim(), htbCriteria, true);
                    if (drwResult != null)
                    {
                        txtLot.Text = drwResult[IV_LocationCacheTable.LOT_FLD].ToString();
                        txtSerial.Text = drwResult[IV_ItemSerialTable.SERIAL_FLD].ToString();
                        txtAvailQuantity.Value = drwResult[AVAILABLE_QUANTITY];
                    }
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtLocation_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Thursday, July 28 2005</date>
        private void txtLocation_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtLocation_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnLocation_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtBin_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Thursday, July 28 2005</date>
        private void txtBin_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtBin_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnBin_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtLot_Leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Thursday, July 28 2005</date>
        private void txtLot_Leave(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtLot_Leave()";
            try
            {
                if (!txtLot.Modified) return;
                if (txtLot.Text.Trim() == string.Empty)
                {
                    txtSerial.Text = string.Empty;
                    return;
                }
                Hashtable htbCriteria = new Hashtable();
                DataRowView drwResult = null;
                if (blnBinControl)
                {
                    if (txtBin.Text == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_MUST_SELECT_BIN_FOR_LOCATION, MessageBoxIcon.Warning);
                        txtLot.Text = string.Empty;
                        txtBin.Focus();
                        return;
                    }
                    else
                        htbCriteria.Add(MST_BINTable.BINID_FLD, int.Parse(txtBin.Tag.ToString()));
                    htbCriteria.Add(ITM_ProductTable.PRODUCTID_FLD, voIV_Adjustment.ProductID);
                    drwResult = FormControlComponents.OpenSearchForm(V_LOTBYBIN, IV_BinCacheTable.LOT_FLD, txtLot.Text.Trim(), null, false);
                    if (drwResult != null)
                    {
                        txtLot.Text = drwResult[IV_BinCacheTable.LOT_FLD].ToString();
                        txtSerial.Text = drwResult[IV_ItemSerialTable.SERIAL_FLD].ToString();
                        txtAvailQuantity.Value = drwResult[ISSUE_QUANTITY];
                    }
                    else
                        txtLot.Focus();
                }
                else
                {
                    if (txtLocation.Text == string.Empty)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_MATERIALRECEIPT_SELECT_LOCATION, MessageBoxIcon.Warning);
                        txtLocation.Focus();
                        return;
                    }
                    else
                        htbCriteria.Add(MST_LocationTable.LOCATIONID_FLD, int.Parse(txtLocation.Tag.ToString()));
                    htbCriteria.Add(ITM_ProductTable.PRODUCTID_FLD, voIV_Adjustment.ProductID);
                    drwResult = FormControlComponents.OpenSearchForm(V_LOTBYLOC, IV_LocationCacheTable.LOT_FLD, txtLot.Text.Trim(), htbCriteria, false);
                    if (drwResult != null)
                    {
                        txtLot.Text = drwResult[IV_LocationCacheTable.LOT_FLD].ToString();
                        txtSerial.Text = drwResult[IV_ItemSerialTable.SERIAL_FLD].ToString();
                        txtAvailQuantity.Value = drwResult[AVAILABLE_QUANTITY];
                    }
                    else
                        txtLot.Focus();
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtLot_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Friday, July 29 2005</date>
        private void txtLot_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtLot_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnLot_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// txtTransNo_Leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Thursday, July 28 2005</date>
        private void txtTransNo_Leave(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnTransNo_Click()";
            try
            {
                if (!txtTransNo.Modified) return;
                if (formMode == EnumAction.Add)
                {
                    return;
                }
                if (txtTransNo.Text.Trim() == string.Empty)
                {
                    ClearForm();
                    dtmPostDate.Value = DBNull.Value;
                    return;
                }
                Hashtable htbCriteria = new Hashtable();
                DataRowView drwResult = null;
                if (cboCCN.SelectedValue != null)
                {
                    htbCriteria.Add(IV_AdjustmentTable.CCNID_FLD, cboCCN.SelectedValue);
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                }
                drwResult = FormControlComponents.OpenSearchForm(V_IVADJUSTMENTANDPRODUCT, IV_AdjustmentTable.TRANSNO_FLD, txtTransNo.Text.Trim(), htbCriteria, false);
                if (drwResult != null)
                {
                    //HACK: added by Tuan TQ- 12 July, 2006- Store TransID for printing
                    txtTransNo.Tag = drwResult[IV_AdjustmentTable.ADJUSTMENTID_FLD];
                    //End hack

                    txtTransNo.Text = drwResult[IV_AdjustmentTable.TRANSNO_FLD].ToString();
                    txtComment.Text = drwResult[IV_AdjustmentTable.COMMENT_FLD].ToString();
                    dtmPostDate.Value = drwResult[IV_AdjustmentTable.POSTDATE_FLD];
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                    txtLocation.Text = drwResult[LOCNAME].ToString();
                    txtUM.Text = drwResult[UM].ToString();
                    if (drwResult[BINNAME] != DBNull.Value)
                    {
                        txtBin.Text = drwResult[BINNAME].ToString();
                    }
                    if (drwResult[IV_AdjustmentTable.LOT_FLD] != DBNull.Value)
                    {
                        txtLot.Text = drwResult[IV_AdjustmentTable.LOT_FLD].ToString();
                    }
                    if (drwResult[IV_AdjustmentTable.SERIAL_FLD] != DBNull.Value)
                    {
                        txtSerial.Text = drwResult[IV_AdjustmentTable.SERIAL_FLD].ToString();
                    }
                    txtAdjustQuantity.Value = drwResult[IV_AdjustmentTable.ADJUSTQUANTITY_FLD];
                    txtAvailQuantity.Value = drwResult[IV_AdjustmentTable.AVAILABLEQTY_FLD];
                }
                else
                    txtTransNo.Focus();
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// txtTransNo_KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Thursday, July 28 2005</date>
        private void txtTransNo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtTransNo_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                {
                    btnTransNo_Click(sender, e);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }

        }
        /// <summary>
        /// IVInventoryAdjustment_Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <author>Trada</author>
        /// <date>Tuesday, October 18 2005</date>
        private void IVInventoryAdjustment_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".IVInventoryAdjustment_Closing()";
            try
            {
                if (formMode != EnumAction.Default)
                {
                    DialogResult confirmDialog = PCSMessageBox.Show(ErrorCode.MESSAGE_QUESTION_STORE_INTO_DATABASE, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (confirmDialog)
                    {
                        case DialogResult.Yes:
                            //Save before exit
                            btnSave_Click(btnSave, new EventArgs());
                            if (blnHasError)
                            {
                                e.Cancel = true;
                            }
                            break;
                        case DialogResult.No:
                            break;
                        case DialogResult.Cancel:
                            e.Cancel = true;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void dtmPostDate_Leave(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".dtmPostDate_Leave()";
            try
            {
                if (txtPartName.Text.Trim() != string.Empty
                    && cboCCN.SelectedValue != DBNull.Value
                    && txtMasLoc.Text.Trim() != string.Empty
                    && txtLocation.Text.Trim() != string.Empty)
                {
                    if (dtmPostDate.Text.Trim() != string.Empty)
                    {
                        if (txtBin.Text.Trim() == string.Empty)
                        {
                            //							txtAvailQuantity.Value = new InventoryUtilsBO().GetAvailableQtyByPostDate((DateTime)dtmPostDate.Value, int.Parse(cboCCN.SelectedValue.ToString()),
                            //								voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), 0, voIV_Adjustment.ProductID);
                            txtAvailQuantity.Value = new InventoryUtilsBO().GetAvailableQtyByPostDate(dtmCurrentDate, int.Parse(cboCCN.SelectedValue.ToString()),
                                voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), 0, voIV_Adjustment.ProductID);
                        }
                        else
                            //							txtAvailQuantity.Value = new InventoryUtilsBO().GetAvailableQtyByPostDate((DateTime)dtmPostDate.Value, int.Parse(cboCCN.SelectedValue.ToString()),
                            //								voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), (int) txtBin.Tag, voIV_Adjustment.ProductID);
                            txtAvailQuantity.Value = new InventoryUtilsBO().GetAvailableQtyByPostDate(dtmCurrentDate, int.Parse(cboCCN.SelectedValue.ToString()),
                                voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), (int)txtBin.Tag, voIV_Adjustment.ProductID);
                    }
                }
            }
            catch (Exception ex)
            {
                // error from date time control when user select date out of range
                if (ex.Message.Equals(dtmPostDate.PostValidation.ErrorMessage))
                    return;
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtPartNumber_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtPartNumber_Validating()";
            try
            {
                if (txtPartNumber.Text.Trim() == string.Empty)
                {
                    ClearSomeControls();
                    txtPartName.Text = string.Empty;
                    txtModel.Text = string.Empty;
                    return;
                }
                if (!txtPartNumber.Modified) return;

                if (FormControlComponents.CheckMandatory(dtmPostDate))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return;
                }
                //Search Part Name
                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(V_PRODUCTINVENTORYADJUSTMENT, PARTNUMBER, txtPartNumber.Text.Trim(), null, false);
                if (drwResult != null)
                {
                    voIV_Adjustment.ProductID = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    if (drwResult[MASLOCNAME] != DBNull.Value)
                    {
                        voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());
                        txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                    }
                    else
                        voIV_Adjustment.MasterLocationID = 0;
                    if (drwResult[UM] != DBNull.Value)
                    {
                        txtUM.Text = drwResult[UM].ToString();
                        voIV_Adjustment.StockUMID = int.Parse(drwResult[ITM_ProductTable.STOCKUMID_FLD].ToString());
                    }
                    //Check Available if have Bin and Location
                    FillAvailableQuantity(voIV_Adjustment.ProductID);
                }
                else
                    e.Cancel = true;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtMasLoc_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtMasLoc_Validating()";
            try
            {
                if (!txtMasLoc.Modified) return;
                if (txtMasLoc.Text.Trim() == string.Empty)
                {
                    voIV_Adjustment.MasterLocationID = 0;
                    txtMasLoc.Tag = null;
                    lblMasLoc.Tag = null;
                    txtLocation.Text = string.Empty;
                    txtLocation.Tag = null;
                    txtBin.Tag = null;
                    txtBin.Text = string.Empty;
                    txtAvailQuantity.Value = null;
                    return;
                }
                DataRowView drwResult = null;
                Hashtable htbCriteria = new Hashtable();

                drwResult = FormControlComponents.OpenSearchForm(MST_MasterLocationTable.TABLE_NAME, MST_MasterLocationTable.CODE_FLD, txtMasLoc.Text.Trim(), htbCriteria, false);
                if (drwResult != null)
                {
                    if (voIV_Adjustment.MasterLocationID != 0)
                    {
                        if (voIV_Adjustment.MasterLocationID != int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString()))
                        {
                            ClearSomeControls();
                        }
                    }
                    //lblMasLoc.Tag = drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD];
                    voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());

                    txtMasLoc.Text = drwResult[MST_MasterLocationTable.CODE_FLD].ToString();
                }
                else
                    e.Cancel = true;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtLocation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtLocation_Validating()";
            try
            {
                if (!txtLocation.Modified) return;
                Hashtable htbCriteria = new Hashtable();
                if (txtLocation.Text.Trim() == string.Empty)
                {
                    txtLocation.Tag = null;
                    txtBin.Text = string.Empty;
                    txtBin.Tag = null;
                    ClearSomeControls();
                    return;
                }
                if (txtMasLoc.Text.Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MSG_LABORTIME_SELECTMASLOC, MessageBoxIcon.Error);
                    txtLocation.Text = string.Empty;
                    txtMasLoc.Focus();
                    txtMasLoc.Select();
                    return;
                }
                else
                    htbCriteria.Add(MST_LocationTable.MASTERLOCATIONID_FLD, voIV_Adjustment.MasterLocationID);

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(MST_LocationTable.TABLE_NAME, MST_LocationTable.CODE_FLD, txtLocation.Text.Trim(), htbCriteria, false);
                if (drwResult != null)
                {
                    if (txtLocation.Tag != null)
                    {
                        if (txtLocation.Tag.ToString() != drwResult[MST_LocationTable.LOCATIONID_FLD].ToString())
                        {

                            txtLocation.Tag = drwResult[MST_LocationTable.LOCATIONID_FLD];
                            //ClearForm();
                            ClearSomeControls();
                        }
                    }
                    txtLocation.Tag = drwResult[MST_LocationTable.LOCATIONID_FLD];
                    txtLocation.Text = drwResult[MST_LocationTable.CODE_FLD].ToString();
                    //Check if bin control is true
                    //					if (drwResult[MST_LocationTable.BIN_FLD].ToString() == TRUE)
                    //					{
                    //						//Clear Bin, QA Status, Available Quantity
                    //						txtQAStatus.Text = string.Empty;
                    //						txtAvailQuantity.Value = null;
                    //						blnBinControl = true;
                    //					}
                    //					else 
                    //					{
                    //						blnBinControl = false;
                    //						txtBin.Text = string.Empty;
                    //						txtBin.Tag = null;
                    //						txtBin.Enabled = false;
                    //						btnBin.Enabled = false;
                    //
                    //						//Get available quantity where product
                    //						IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    //						dstAvailableQtyAndInsStatus = boIVInventoryAdjustment.GetAvalableQuantity(int.Parse(cboCCN.SelectedValue.ToString()),
                    //							voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), 0, voIV_Adjustment.ProductID);
                    //						if (dstAvailableQtyAndInsStatus.Tables[0].Rows.Count > 0)
                    //						{
                    //							txtAvailQuantity.Value = dstAvailableQtyAndInsStatus.Tables[0].Rows[0][AVAILABLE_QUANTITY];
                    //							txtQAStatus.Text = dstAvailableQtyAndInsStatus.Tables[0].Rows[0][IV_LocationCacheTable.INSPSTATUS_FLD].ToString();
                    //						}
                    //						else
                    //						{
                    //							txtAvailQuantity.Value = 0;
                    //							txtQAStatus.Text = string.Empty;
                    //						}
                    //					}
                }
                else
                    e.Cancel = true;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtBin_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtBin_Validating()";
            try
            {
                Hashtable htbCriteria = new Hashtable();
                if (!txtBin.Modified) return;
                if (txtBin.Text.Trim() == string.Empty)
                {
                    txtBin.Tag = null;
                    txtAvailQuantity.Value = null;
                    txtQAStatus.Text = string.Empty;
                    return;
                }
                if (txtLocation.Text.Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_SELECTLOC, MessageBoxIcon.Error);
                    txtBin.Text = string.Empty;
                    txtLocation.Focus();
                    txtLocation.Select();
                    return;
                }
                else
                    htbCriteria.Add(MST_BINTable.LOCATIONID_FLD, int.Parse(txtLocation.Tag.ToString()));

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(MST_BINTable.TABLE_NAME, MST_BINTable.CODE_FLD, txtBin.Text.Trim(), htbCriteria, false);
                if (drwResult != null)
                {
                    txtBin.Text = drwResult[MST_BINTable.CODE_FLD].ToString();
                    txtBin.Tag = drwResult[MST_BINTable.BINID_FLD];

                    //Get available quantity where product
                    //					IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    //					dstAvailableQtyAndInsStatus = boIVInventoryAdjustment.GetAvalableQuantity(int.Parse(cboCCN.SelectedValue.ToString()),
                    //						voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), int.Parse(txtBin.Tag.ToString()), voIV_Adjustment.ProductID);
                    //					
                    if (txtPartNumber.Text.Trim() != string.Empty)
                    {
                        decimal decAvailableQuantity = new InventoryUtilsBO().GetAvailableQtyByPostDate(dtmCurrentDate, int.Parse(cboCCN.SelectedValue.ToString()),
                            voIV_Adjustment.MasterLocationID, int.Parse(txtLocation.Tag.ToString()), (int)txtBin.Tag, voIV_Adjustment.ProductID);
                        txtAvailQuantity.Value = decAvailableQuantity;
                    }
                    //					if (dstAvailableQtyAndInsStatus.Tables[0].Rows.Count > 0)
                    //					{
                    //						txtAvailQuantity.Value = dstAvailableQtyAndInsStatus.Tables[0].Rows[0][AVAILABLE_QUANTITY];
                    //						txtQAStatus.Text = dstAvailableQtyAndInsStatus.Tables[0].Rows[0][IV_LocationCacheTable.INSPSTATUS_FLD].ToString();
                    //					}
                    //					else
                    //					{
                    //						txtAvailQuantity.Value = decAvailableQuantity;
                    //						txtQAStatus.Text = string.Empty;
                    //					}

                }
                else
                    e.Cancel = true;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtPartName_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtPartName_Validating()";
            try
            {
                if (txtPartName.Text.Trim() == string.Empty)
                {
                    ClearSomeControls();
                    txtPartNumber.Text = string.Empty;
                    txtModel.Text = string.Empty;
                    return;
                }

                if (!txtPartName.Modified) return;
                if (FormControlComponents.CheckMandatory(dtmPostDate))
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID);
                    dtmPostDate.Focus();
                    dtmPostDate.Select();
                    return;
                }
                //Search Part Name
                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(V_PRODUCTINVENTORYADJUSTMENT, PARTNAME, txtPartName.Text.Trim(), null, false);
                if (drwResult != null)
                {
                    voIV_Adjustment.ProductID = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    txtPartNumber.Text = drwResult[PARTNUMBER].ToString();
                    txtPartName.Text = drwResult[PARTNAME].ToString();
                    txtModel.Text = drwResult[MODEL].ToString();
                    if (drwResult[MASLOCNAME] != DBNull.Value)
                    {
                        voIV_Adjustment.MasterLocationID = int.Parse(drwResult[MST_MasterLocationTable.MASTERLOCATIONID_FLD].ToString());
                        txtMasLoc.Text = drwResult[MASLOCNAME].ToString();
                    }
                    if (drwResult[UM] != DBNull.Value)
                    {
                        txtUM.Text = drwResult[UM].ToString();
                        voIV_Adjustment.StockUMID = int.Parse(drwResult[ITM_ProductTable.STOCKUMID_FLD].ToString());
                    }
                    //Check Available if have Bin and Location
                    FillAvailableQuantity(voIV_Adjustment.ProductID);
                }
                else
                    e.Cancel = true;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnDelete_Click()";
            try
            {
                if (PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Delete voucher
                    IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    boIVInventoryAdjustment.DeleteInventoryAdjustment((int)txtTransNo.Tag);

                    // Reset form IVInventoryAdjustment
                    ClearForm();
                    SwitchFormMode();

                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);

                    //Set focus to PostDate
                    dtmPostDate.Focus();

                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnDelTran_Click(object sender, System.EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnDelete_Click()";
            try
            {
                if (PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Delete voucher
                    IVInventoryAdjustmentBO boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    boIVInventoryAdjustment.DeleteInventoryAdjustmentTransaction((int)txtTransNo.Tag);

                    // Reset form IVInventoryAdjustment
                    ClearForm();
                    SwitchFormMode();

                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);

                    //Set focus to PostDate
                    dtmPostDate.Focus();
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void UpdateInventoryData_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".UpdateInventoryData_Click()";
            try
            {
                if (PCSMessageBox.Show(UpdateInventoryText.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // update inventory for all location and master location based on quantity in Bin
                    var boIVInventoryAdjustment = new IVInventoryAdjustmentBO();
                    boIVInventoryAdjustment.UpdateInventoryData();

                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);
                }
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

    }
}
